//
//  BYBaseViewController.m
//  BYControls
//
//  Created by 彬boy on 2018/11/14.
//  Copyright © 2018年 彬boy. All rights reserved.
//

#import "BYBaseViewController.h"

@interface BYBaseViewController ()

@end

@implementation BYBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.by_navBarControl];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)leftBtnOnClick {
    if (self.navigationController.viewControllers.count > 1) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.view bringSubviewToFront:self.by_navBarControl];
    if (self.by_navBarControl.hidden == NO) {
        CGFloat navigationBarY = 0.0f;
        if (self.view.bounds.size.width > self.view.bounds.size.height) {// 横屏
            navigationBarY = -StatusBarHeight;
        }// 模仿系统横屏时隐藏状态栏, navigationBar上移
        self.by_navBarControl.frame = CGRectMake(0.0f, navigationBarY, self.view.bounds.size.width, NavigationBarHeight);
    }
}

// 隐藏自定义的导航栏
- (void)setBY_navigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    
    if (hidden) {
        CGRect frame = self.by_navBarControl.frame;
        frame.origin.y = -frame.size.height;
        CGFloat duration = animated ? 0.25f : 0.0f;
        [UIView animateWithDuration:duration animations:^{
            self.by_navBarControl.frame = frame;
            
        } completion:^(BOOL finished) {
            // 动画完成再隐藏, 当然不隐藏也没有关系, 因为已经移动到了屏幕外面
            self.by_navBarControl.hidden = hidden;
            
        }];
    }
    else {
        CGRect frame = self.by_navBarControl.frame;
        frame.origin.y = 0.0f;
        if (self.view.bounds.size.width > self.view.bounds.size.height) {// 横屏
            frame.origin.y = -StatusBarHeight;
        }
        CGFloat duration = animated ? 0.25f : 0.0f;
        [UIView animateWithDuration:duration animations:^{
            self.by_navBarControl.frame = frame;
            self.by_navBarControl.hidden = hidden;
        }];
    }
}

- (void)setStatusBarBackgroundColor:(UIColor *)color {
    if (self.by_navBarControl) {
        self.by_navBarControl.statusBarBackgroundColor = color;
    }
}

- (BYNavigationBarView *)by_navBarControl {
    if (!_by_navBarControl) {
        _by_navBarControl = [[BYNavigationBarView alloc] init];
        _by_navBarControl.backgroundColor = [UIColor redColor];
    }
    return _by_navBarControl;
}


@end
